<?php

class Messages extends CI_Controller {
	
	 public function __construct(){
		parent::__construct();		
		if(!is_admin_role('admin'))
			redirect(base_url('admin/login'));
		$this->load->model('message_m', 'message');
		$this->load->helper('util');
    }
	
	function index() {
		$data = array();
		$data['messages']		= $this->message->get_messages();
		$data['main_content'] = 'admin/messages';
      $this->load->view('admin/includes/template', $data);
	}
	
	function form($id = false){
		$this->load->library('form_validation');
		//
		$data['ckeditor']	=	array(
			//ID of the textarea that will be replaced
			'id' 	=> 	'content',
			'path'	=>	'assets/ckeditor',
			//Optionnal values
			'config' => array(
				'toolbar' 	=> 	"Full", 	//Using the Full toolbar
				'width' 		=> 	"100%",	//Setting a custom width
				'height' 	=> 	'300px',	//Setting a custom height
			),
			//Replacing styles from the "Styles tool"
			'styles' => array(
				//Creating a new style named "style 1"
				'style 1' => array (
					'name' 		=> 	'Blue Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 	=> 	'Blue',
						'font-weight' 	=> 	'bold'
					)
				),
				//Creating a new style named "style 2"
				'style 2' => array (
					'name' 	=> 	'Red Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 		=> 	'Red',
						'font-weight' 		=> 	'bold',
						'text-decoration'	=> 	'underline'
					)
				)				
			)
		);
		//set the default values
		$data['id']				= '';
		$data['name']			= '';
		$data['slug']			= '';
		$data['sequence']		= 0;
		$data['content']		= '';
		$data['subject']		= '';
		$data['page_title']	= 'Message Form';
		
		if($id){
			$message			= $this->message->get_message($id);
			if(!$message){
				//message does not exist
				$this->session->set_flashdata('error', 'Message không tìm thấy');
				redirect('admin/messages');
			}
			//set values to db values
			$data['id']				= $message->id;
			$data['name']			= $message->name;
			$data['slug']			= $message->slug;
			$data['subject']		= $message->subject;
			$data['content']		= $message->content;
			$data['deletable']	= $message->deletable;
			$data['sequence']		= $message->sequence;
		}
		
		$this->form_validation->set_rules('name', 'lang:title', 'trim|required');
		$this->form_validation->set_rules('slug', 'lang:slug', 'trim');
		$this->form_validation->set_rules('subject', 'lang:subject', 'trim');
		$this->form_validation->set_rules('sequence', 'lang:sequence', 'trim|integer');
		$this->form_validation->set_rules('content', 'lang:content', 'trim');
		
		// Validate the form
		if($this->form_validation->run() == false)
		{
			$data['main_content'] = 'admin/message_form';
			$this->load->view('admin/includes/template', $data);
		}
		else
		{
			$this->load->helper('text');			
			//first check the slug field
			$slug = $this->input->post('slug');			
			//if it's empty assign the name field
			if(empty($slug) || $slug=='')
				$slug = $this->input->post('name');
			
			$slug	= remove_accents($slug);
			$save = array();
			$save['id']			= $id;
			$save['name']		= $this->input->post('name');
			$save['subject']	= $this->input->post('subject'); 
			$save['sequence']	= $this->input->post('sequence');
			$save['content']	= $this->input->post('content');
			$save['slug']		= $slug;
			
			//save the message
			$page_id	= $this->message->save($save);
			
			$this->session->set_flashdata('message', 'Lưu dữ liệu thành công');
			
			//go back to the message list
			redirect('admin/messages');
		}
	}
	
	function delete($id){
		$message	= $this->message->get_message($id);
		if($message){
			$this->message->delete_message($id);
			$this->session->set_flashdata('message', 'Xóa thành công');
		}else{
			$this->session->set_flashdata('error', 'Có lỗi xảy ra');
		}
		redirect('admin/messages');
	}
}